﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;


namespace CRM.Web.SurveyManage
{
    public partial class SurveyShow : Common.PageBase
    {
        UserLoginInfo _userLoginInfo;
        T_SURVEY_BLL sBll = new T_SURVEY_BLL();
        T_SURVEY_ITEM_BLL iBll = new T_SURVEY_ITEM_BLL();
        T_SURVEY_ITEM_OPTION_BLL siopBll = new T_SURVEY_ITEM_OPTION_BLL();
        T_SURVEY_RESULT_BLL resBll = new T_SURVEY_RESULT_BLL();
        Hashtable htCtrs = new Hashtable();
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);
            LoadProperty();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }
        #region 页面初始化数据绑定
        #endregion

        private void LoadProperty()
        {
            string _surveyid = String.IsNullOrEmpty(Request.QueryString["SURVEY_ID"]) ? "" : Request.QueryString["SURVEY_ID"].Trim();
            if (!String.IsNullOrEmpty(_surveyid))
            {
                #region 加载问卷题目
                T_SURVEY_Entity s_entity = sBll.GetEntity("SURVEY_ID", _surveyid);
                if (s_entity != null)
                {
                    this.lblTitle.Text = s_entity.SNAME.ToString();
                    TableRow row = new TableRow();
                    TableCell cell = new TableCell();
                    Label lbitem = new Label();
                    List<T_SURVEY_ITEM_Entity> iList = new List<T_SURVEY_ITEM_Entity>();
                    iList = iBll.GetList(String.Format(" AND SURVEY_ID={0} AND BUSINESSDIVISIONCODE='{1}' ORDER BY ITEM_ID ASC", s_entity.SURVEY_ID, s_entity.BUSINESSDIVISIONCODE));
                    if (iList.Count > 0)
                    {
                        List<T_SURVEY_ITEM_OPTION_Entity> siopList = new List<T_SURVEY_ITEM_OPTION_Entity>();
                        foreach (T_SURVEY_ITEM_Entity i_entity in iList)
                        {
                            //增加题目名称
                            row = new TableRow();
                            cell = new TableCell();
                            lbitem = new Label();
                            lbitem.ID = i_entity.ITEM_ID.ToString();
                            lbitem.Text = i_entity.ITEM_ID.ToString()+"、"+i_entity.ITEM_NAME;
                            cell.CssClass = "title";
                            cell.Font.Bold = true;
                            cell.Controls.Add(lbitem);
                            row.Cells.Add(cell);
                            this.tbSurvey.Rows.Add(row);
                            htCtrs.Add(lbitem.ID, lbitem.Text);
                            siopList = siopBll.GetList(String.Format(" AND SURVEY_ID={0} AND ITEM_ID={1} AND BUSINESSDIVISIONCODE='{2}' ORDER BY OPTION_ID ASC", s_entity.SURVEY_ID, i_entity.ITEM_ID, s_entity.BUSINESSDIVISIONCODE));
                            if (siopList.Count > 0)
                            {
                                row = new TableRow();
                                cell = new TableCell();
                                foreach (T_SURVEY_ITEM_OPTION_Entity o_entity in siopList)
                                {
                                    // 增加题目选项
                                    if (i_entity.ITEM_TYPE == 0)//单选
                                    {
                                        RadioButton rb = new RadioButton();
                                        //rb.ID = "rb" + s_entity.SURVEY_ID + i_entity.ITEM_ID +"|"+ o_entity.OPTION_ID;
                                        rb.ID = i_entity.ITEM_ID + "|" + o_entity.OPTION_ID;
                                        rb.GroupName = "rb" + s_entity.SURVEY_ID + i_entity.ITEM_ID;
                                        rb.ValidationGroup = "rb" + s_entity.SURVEY_ID + i_entity.ITEM_ID;
                                        rb.Text = o_entity.OPTION_NAME;
                                        rb.Style.Add("margin", "0px 10px 0px 20px");
                                        cell.CssClass = "content";
                                        //cell.Style.Add("padding-left", "10px");
                                        cell.Controls.Add(rb);
                                        row.Cells.Add(cell);
                                        this.tbSurvey.Rows.Add(row);
                                        htCtrs.Add(rb.ID, rb.Text);
                                    }
                                    else if (i_entity.ITEM_TYPE == 1)//多选
                                    {
                                        CheckBox chb = new CheckBox();
                                        //chb.ID = "chb" + s_entity.SURVEY_ID + i_entity.ITEM_ID +"|"+ o_entity.OPTION_ID;
                                        chb.ID = i_entity.ITEM_ID + "|" + o_entity.OPTION_ID;
                                        chb.Text = o_entity.OPTION_NAME;
                                        chb.Style.Add("margin", "0px 10px 0px 20px");
                                        cell.CssClass = "content";
                                        cell.Controls.Add(chb);
                                        row.Cells.Add(cell);
                                        this.tbSurvey.Rows.Add(row);
                                        htCtrs.Add(chb.ID, chb.Text);
                                    }
                                }
                            }
                            else if (i_entity.ITEM_TYPE == 2)//问答
                            {
                                row = new TableRow();
                                cell = new TableCell();
                                TextBox txt = new TextBox();
                                //txt.ID = "txt" + s_entity.SURVEY_ID + i_entity.ITEM_ID;
                                txt.ID = i_entity.ITEM_ID + "|";
                                txt.CssClass = "txt txt90";
                                txt.Height=50;
                                txt.TextMode = TextBoxMode.MultiLine;
                                cell.CssClass = "content";
                                cell.Style.Add("padding-left", "20px");
                                cell.Controls.Add(txt);
                                row.Cells.Add(cell);
                                this.tbSurvey.Rows.Add(row);
                                htCtrs.Add(txt.ID, txt.Text);
                            }
                        }
                    }
                }
                #endregion
                
            }
        }

        private void pageDataInit()
        {
            string _taskid = String.IsNullOrEmpty(Request.QueryString["TASKID"]) ? "" : Request.QueryString["TASKID"].Trim();
            string _surveyid = String.IsNullOrEmpty(Request.QueryString["SURVEY_ID"]) ? "" : Request.QueryString["SURVEY_ID"].Trim();
            string _customerid = String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]) ? "" : Request.QueryString["CUSTOMERID"].Trim();
            #region 加载问卷答案
            List<T_SURVEY_RESULT_Entity> resList = new List<T_SURVEY_RESULT_Entity>();
            T_SURVEY_RESULT_Entity resEntity = new T_SURVEY_RESULT_Entity();
            resList = resBll.GetList(String.Format(" AND TASKID='{0}' AND CUSTOMERID='{1}' AND SURVEY_ID='{2}' AND BUSINESSDIVISIONCODE='{3}' ", _taskid, _customerid, _surveyid, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim()));
            foreach (T_SURVEY_RESULT_Entity r_entity in resList)
            {
                if (r_entity.ITEM_TYPE == 0)
                {
                    string id=r_entity.ITEM_ID.ToString()+"|"+r_entity.ITEM_RESULT.ToString();
                    Control ctr = this.Page.FindControl(id);
                    if (ctr == null) continue;
                    RadioButton controlRadioButtonObj = (RadioButton)ctr;
                    controlRadioButtonObj.Checked = true;
                }
                else if (r_entity.ITEM_TYPE == 1)
                {
                    string[] res = r_entity.ITEM_RESULT.Split('|');
                    if (res.Length == 0) continue;
                    foreach (string str in res)
                    {
                        string id = r_entity.ITEM_ID.ToString() + "|" + str;
                        Control ctr = this.Page.FindControl(id);
                        if (ctr == null) continue;
                        CheckBox controlCheckBoxObj = (CheckBox)ctr;
                        controlCheckBoxObj.Checked = true;
                    }
                }
                else
                {
                    string id = r_entity.ITEM_ID.ToString() + "|";
                    Control ctr = this.Page.FindControl(id);
                    if (ctr == null) continue;
                    TextBox controlTextBoxObj = (TextBox)ctr;
                    controlTextBoxObj.Text = r_entity.ITEM_RESULT;
                }
            }
            #endregion
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            //读取界面控件，保存控件值
            Hashtable htCtl = getAllControlValue(htCtrs);
            if (htCtl.Count <= 0) return;
            string _taskid=String.IsNullOrEmpty(Request.QueryString["TASKID"]) ? "" : Request.QueryString["TASKID"].Trim();
            string _surveyid = String.IsNullOrEmpty(Request.QueryString["SURVEY_ID"]) ? "" : Request.QueryString["SURVEY_ID"].Trim();
            string _customerid=String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]) ? "" : Request.QueryString["CUSTOMERID"].Trim();
            if (!String.IsNullOrEmpty(_surveyid))
            {
                T_SURVEY_Entity s_entity = sBll.GetEntity("SURVEY_ID", _surveyid);
                List<T_SURVEY_RESULT_Entity> resList=new List<T_SURVEY_RESULT_Entity>();
                List<T_SURVEY_ITEM_Entity> iList = new List<T_SURVEY_ITEM_Entity>();
                iList = iBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND BUSINESSDIVISIONCODE='{1}' ORDER BY ITEM_ID ASC", s_entity.SURVEY_ID, s_entity.BUSINESSDIVISIONCODE));
                if (iList.Count > 0)
                {
                    T_SURVEY_RESULT_Entity resEntity=new T_SURVEY_RESULT_Entity();
                    List<T_SURVEY_ITEM_OPTION_Entity> siopList = new List<T_SURVEY_ITEM_OPTION_Entity>();
                    foreach (T_SURVEY_ITEM_Entity i_entity in iList)
                    {
                        resEntity = resBll.GetList(String.Format(" AND TASKID='{0}' AND CUSTOMERID='{1}' AND SURVEY_ID='{2}' AND ITEM_ID='{3}' AND BUSINESSDIVISIONCODE='{4}' ", _taskid, _customerid, _surveyid, i_entity.ITEM_ID, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim())).FirstOrDefault();
                        if (resEntity == null)
                        {
                            resEntity = new T_SURVEY_RESULT_Entity();
                            if (i_entity.ITEM_TYPE <= 1)
                            {
                                siopList = new List<T_SURVEY_ITEM_OPTION_Entity>();
                                siopList = siopBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}' AND BUSINESSDIVISIONCODE='{2}' ORDER BY OPTION_ID ASC", s_entity.SURVEY_ID, i_entity.ITEM_ID, s_entity.BUSINESSDIVISIONCODE));
                                if (siopList.Count <= 0) continue;
                                foreach (T_SURVEY_ITEM_OPTION_Entity o_entity in siopList)
                                {
                                    string id = i_entity.ITEM_ID + "|" + o_entity.OPTION_ID;
                                    if (!string.IsNullOrEmpty(htCtl[id].ToString()))
                                    {
                                        resEntity.ITEM_RESULT = string.IsNullOrEmpty(resEntity.ITEM_RESULT) ? htCtl[id].ToString() : resEntity.ITEM_RESULT + "|" + htCtl[id].ToString();
                                    }
                                }
                            }
                            else
                            {
                                resEntity.ITEM_RESULT = htCtl[i_entity.ITEM_ID + "|"].ToString();
                            }
                            resEntity.TASKID = _taskid;
                            resEntity.SURVEY_ID = int.Parse(_surveyid);
                            resEntity.CUSTOMERID = _customerid;
                            resEntity.ITEM_ID = i_entity.ITEM_ID;
                            resEntity.ITEM_TYPE = i_entity.ITEM_TYPE;
                            if (i_entity.ENNULL == 1 && String.IsNullOrEmpty(resEntity.ITEM_RESULT))
                            {
                                if (htCtl.ContainsKey(i_entity.ITEM_ID.ToString()))
                                {
                                    PageHelper.ShowMessage(this.Page, "题目<" + htCtl[i_entity.ITEM_ID.ToString()].ToString() + ">是必填题目，请填写！");
                                    return;
                                }
                            }
                            resEntity.CREATEPEOPLE=_userLoginInfo.UserEntity.USERNAME;
                            resEntity.CREATETIME = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 
                            resEntity.BUSINESSDIVISIONCODE=_userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim();
                            resEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                            resEntity.BUSINESSDIVISIONNAME=_userLoginInfo.UserEntity.BUSINESSDIVISIONNAME.Trim();
                            resBll.Insert(resEntity);
                        }
                        else
                        {
                            resEntity.ITEM_RESULT = "";
                            if (i_entity.ITEM_TYPE <= 1)
                            {
                                siopList = new List<T_SURVEY_ITEM_OPTION_Entity>();
                                siopList = siopBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}' AND BUSINESSDIVISIONCODE='{2}' ORDER BY OPTION_ID ASC", s_entity.SURVEY_ID, i_entity.ITEM_ID, s_entity.BUSINESSDIVISIONCODE));
                                if (siopList.Count <= 0) continue;
                                foreach (T_SURVEY_ITEM_OPTION_Entity o_entity in siopList)
                                {
                                    string id = i_entity.ITEM_ID + "|" + o_entity.OPTION_ID;
                                    if (!string.IsNullOrEmpty(htCtl[id].ToString()))
                                        resEntity.ITEM_RESULT = string.IsNullOrEmpty(resEntity.ITEM_RESULT)? htCtl[id].ToString() : resEntity.ITEM_RESULT + "|" + htCtl[id].ToString();
                                }
                            }
                            else
                            {
                                resEntity.ITEM_RESULT = htCtl[i_entity.ITEM_ID + "|"].ToString();
                            }
                            resEntity.TASKID = _taskid;
                            resEntity.SURVEY_ID = int.Parse(_surveyid);
                            resEntity.CUSTOMERID = _customerid;
                            resEntity.ITEM_ID = i_entity.ITEM_ID;
                            resEntity.ITEM_TYPE = i_entity.ITEM_TYPE;
                            if (i_entity.ENNULL == 1 && String.IsNullOrEmpty(resEntity.ITEM_RESULT))
                            {
                                if (htCtl.ContainsKey(i_entity.ITEM_ID.ToString()))
                                {
                                    PageHelper.ShowMessage(this.Page, "题目<" + htCtl[i_entity.ITEM_ID.ToString()].ToString() + ">是必填题目，请填写！");
                                    return;
                                }
                            }
                            resEntity.LASTMODIFYPEOPLE=_userLoginInfo.UserEntity.USERNAME;
                            resEntity.LASTMODIFYTIME = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 
                            resBll.Update(resEntity);
                        }
                    }
                }
            }
            PageHelper.ShowMessage(this.Page, "提交成功！");
        }

        protected Hashtable getAllControlValue(Hashtable ht)
        {
            Hashtable htCtl = new Hashtable();
            foreach (DictionaryEntry de in ht)
            {
                Control ctr=this.Page.FindControl(de.Key.ToString());
                if (ctr == null) continue;
                Type controlType = ctr.GetType();
                switch (controlType.ToString())
                {
                    case "System.Web.UI.WebControls.TextBox":
                        TextBox controlTextBoxObj = (TextBox)ctr;
                        string controlTextBoxName = controlTextBoxObj.ID;
                        string controlTextBoxValue = controlTextBoxObj.Text;
                        htCtl.Add(controlTextBoxName, controlTextBoxValue);
                        break;
                    case "System.Web.UI.WebControls.Label":
                        Label controlLabelObj = (Label)ctr;
                        string controlLabelName = controlLabelObj.ID;
                        string controlLabelValue = controlLabelObj.Text;
                        htCtl.Add(controlLabelName, controlLabelValue);
                        break;
                    case "System.Web.UI.WebControls.RadioButton":
                        RadioButton controlRadioButtonObj = (RadioButton)ctr;
                        string controlRadioButtonName = controlRadioButtonObj.ID;
                        string controlRadioButtonValue = "";
                        if (controlRadioButtonObj.Checked)
                            controlRadioButtonValue = controlRadioButtonObj.ID.Substring(controlRadioButtonObj.ID.LastIndexOf("|") + 1);
                        htCtl.Add(controlRadioButtonName, controlRadioButtonValue);
                        break;
                    case "System.Web.UI.WebControls.CheckBox":
                        CheckBox controlCheckBoxObj = (CheckBox)ctr;
                        string controlCheckBoxName = controlCheckBoxObj.ID;
                        string controlCheckBoxValue = "";
                        if (controlCheckBoxObj.Checked)
                            controlCheckBoxValue = controlCheckBoxObj.ID.Substring(controlCheckBoxObj.ID.LastIndexOf("|") + 1);
                        htCtl.Add(controlCheckBoxName, controlCheckBoxValue);
                        break;
                    //case "其他类型":
                    //    其它类型 controlTextBoxObj = (其它类型)ctr;
                    //    string controlTextBoxName = controlTextBoxObj.ID;
                    //    string controlTextBoxValue = controlTextBoxObj.Text;
                    //    ht.Add(controlTextBoxName, controlTextBoxValue);
                    //    break;
                    default:
                        break;
                }
            }
            return htCtl;
        }

        protected Hashtable getAllControlValue( object PageOrUserControl )
        {
            Hashtable rtn = new Hashtable();
            foreach (Control ctr in (PageOrUserControl as Page).Controls)
            {
                getControlValue(ctr, rtn);
            }
            return rtn;
        }
        protected void getControlValue(Control ctrIn,Hashtable ht)
        {
            if (ctrIn.Controls.Count > 0)
            {
                foreach (Control ctr in ctrIn.Controls)
                {
                    Type controlType = ctr.GetType();
                    switch (controlType.ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox":
                            TextBox controlTextBoxObj = (TextBox)ctr;
                            string controlTextBoxName = controlTextBoxObj.ID;
                            string controlTextBoxValue = controlTextBoxObj.Text;
                            ht.Add(controlTextBoxName, controlTextBoxValue);
                            break;
                        case "System.Web.UI.WebControls.Label":
                            Label controlLabelObj = (Label)ctr;
                            string controlLabelName = controlLabelObj.ID;
                            string controlLabelValue = controlLabelObj.Text;
                            ht.Add(controlLabelName, controlLabelValue);
                            break;
                        case "System.Web.UI.WebControls.RadioButton":
                            RadioButton controlRadioButtonObj = (RadioButton)ctr;
                            string controlRadioButtonName = controlRadioButtonObj.ID;
                            string controlRadioButtonValue = "";
                            if (controlRadioButtonObj.Checked)
                                controlRadioButtonValue = controlRadioButtonObj.ID.Substring(controlRadioButtonObj.ID.LastIndexOf("|") + 1);
                            ht.Add(controlRadioButtonName, controlRadioButtonValue);
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            RadioButton controlCheckBoxObj = (RadioButton)ctr;
                            string controlCheckBoxName = controlCheckBoxObj.ID;
                            string controlCheckBoxValue = "";
                            if (controlCheckBoxObj.Checked)
                                controlCheckBoxValue = controlCheckBoxObj.ID.Substring(controlCheckBoxObj.ID.LastIndexOf("|") + 1);
                            ht.Add(controlCheckBoxName, controlCheckBoxValue);
                            break;
                        //case "其他类型":
                        //    其它类型 controlTextBoxObj = (其它类型)ctr;
                        //    string controlTextBoxName = controlTextBoxObj.ID;
                        //    string controlTextBoxValue = controlTextBoxObj.Text;
                        //    ht.Add(controlTextBoxName, controlTextBoxValue);
                        //    break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                Type controlType = ctrIn.GetType();
                switch (controlType.ToString())
                {
                    case "System.Web.UI.WebControls.TextBox":
                        TextBox controlTextBoxObj = (TextBox)ctrIn;
                        string controlTextBoxName = controlTextBoxObj.ID;
                        string controlTextBoxValue = controlTextBoxObj.Text;
                        ht.Add(controlTextBoxName, controlTextBoxValue);
                        break;
                    case "System.Web.UI.WebControls.Label":
                        Label controlLabelObj = (Label)ctrIn;
                        string controlLabelName = controlLabelObj.ID;
                        string controlLabelValue = controlLabelObj.Text;
                        ht.Add(controlLabelName, controlLabelValue);
                        break;
                    case "System.Web.UI.WebControls.RadioButton":
                        RadioButton controlRadioButtonObj = (RadioButton)ctrIn;
                        string controlRadioButtonName = controlRadioButtonObj.ID;
                        string controlRadioButtonValue = "";
                        if (controlRadioButtonObj.Checked)
                            controlRadioButtonValue = controlRadioButtonObj.ID.Substring(controlRadioButtonObj.ID.LastIndexOf("|") + 1);
                        ht.Add(controlRadioButtonName, controlRadioButtonValue);
                        break;
                    case "System.Web.UI.WebControls.CheckBox":
                        RadioButton controlCheckBoxObj = (RadioButton)ctrIn;
                        string controlCheckBoxName = controlCheckBoxObj.ID;
                        string controlCheckBoxValue = "";
                        if (controlCheckBoxObj.Checked)
                            controlCheckBoxValue = controlCheckBoxObj.ID.Substring(controlCheckBoxObj.ID.LastIndexOf("|") + 1);
                        ht.Add(controlCheckBoxName, controlCheckBoxValue);
                        break;
                    //case "其他类型":
                    //    其它类型 controlTextBoxObj = (其它类型)ctr;
                    //    string controlTextBoxName = controlTextBoxObj.ID;
                    //    string controlTextBoxValue = controlTextBoxObj.Text;
                    //    ht.Add(controlTextBoxName, controlTextBoxValue);
                    //    break;
                    default:
                        break;
                }
            }
        }
    }
}
